#include "Level.h"

#include "Room.h"
#include "Tile.h"

#include "tinyxml.h"
#include <string>

typedef std::vector<Room*>::iterator iteratorR;

Level::Level(void)
{
}
Level::~Level(void)
{
	for(unsigned int i = 0; i < roomMap.size(); ++i)
		delete roomMap[i];
	roomMap.clear();
}

void Level::AddRoom(int difficult, int room)
{
	Room* R = new Room(difficult, room);

	roomMap.push_back(R);
}

void Level::AddRoom(Room* R)
{
	roomMap.push_back(R);
}

bool Level::LoadOptions(const char* szFileName)
{
	// Create a TinyXML Document
	TiXmlDocument doc;

	// Attempt to load the document from the file
	if(doc.LoadFile(szFileName) == false)
		return false;

	// Access the root node (level_info) in the document
	TiXmlElement* pRoot = doc.RootElement();
	if(pRoot == nullptr)
		return false;

	// Local variables needed
	roomCount = -1;

	// Iterate through the root's children nodes
	TiXmlElement* pTile = pRoot->FirstChildElement("RoomCount");

	while(pTile != nullptr)
	{
		if(pTile->Attribute("count",&roomCount) == nullptr)
			roomCount = -1;

		// Move to the next node in the level_options
		pTile = pTile->NextSiblingElement("level_options");
	}

	if(roomCount == -1)
		return false;

	return true;
}

bool Level::LoadRooms(const char* szFileName)
{
	// Don't attempt if options were not loaded
	if(roomCount == -1)
		return false;

	// Create a TinyXML Document
	TiXmlDocument doc;
	
	// Attempt to load the document from the file
	if(doc.LoadFile(szFileName) == false)
		return false;

	// Access the root node (level_info) in the document
	TiXmlElement* pRoot = doc.RootElement();
	if(pRoot == nullptr)
		return false;

	// Local variables needed
	int roomNumber;
	int tileType;
	int x;
	int y;
	int width;
	int height;
	int difficult;
	int left;
	int top; 
	int bottom;
	int right;

	// Iterate through the root's children nodes
	TiXmlElement* pTile = pRoot->FirstChildElement("Room");

	int count = 0;
	while(pTile != nullptr)
	{
		// Get the room variables
		if(pTile->Attribute("number",&roomNumber) == nullptr)
			roomNumber = -1;
		if(pTile->Attribute("rating",&difficult) == nullptr)
			difficult = -1;

		// Add room
		if(roomNumber != -1)
			AddRoom(difficult, roomNumber);
		else
			return false;

		// Start getting tiles
		pTile = pTile->NextSiblingElement("Tile");
		while(pTile != nullptr)
		{
			if(pTile->Attribute("type",&tileType) == nullptr)
				tileType = -1;
			if(pTile->Attribute("x",&x) == nullptr)
				x = -1;
			if(pTile->Attribute("y",&y) == nullptr)
				y = -1;
			if(pTile->Attribute("width",&width) == nullptr)
				width = -1;
			if(pTile->Attribute("height",&height) == nullptr)
				height = -1;
			if(pTile->Attribute("left",&left) == nullptr)
				left = -1;
			if(pTile->Attribute("top",&top) == nullptr)
				top = -1;
			if(pTile->Attribute("right",&right) == nullptr)
				right = -1;
			if(pTile->Attribute("bottom",&bottom) == nullptr)
				bottom = -1;
			
			const char* image = pTile->GetText();

			std::string ill = "ERROR";

			if(tileType != 3)
				ill = "graphics/";
			else
				ill = "";

			ill.append(image);

			roomMap[count]->AddTile(tileType, x, y, width, height, ill.c_str(), left, top, right, bottom);

			// Move to the next node in the next tile
			if(pTile->NextSibling() != pTile->NextSiblingElement("Room"))
				pTile = pTile->NextSiblingElement("Tile");
			else
				break;
		}

		count += 1;
		if(roomCount > count)
			pTile = pTile->NextSiblingElement("Room");
	}
	
	return true;
}